const Koa = require('koa')
const KoaLogger = require('koa-session')
const session = require('koa-logger')
const render = require('koa-ejs')
const path = require('path')
const cors =  require('koa-cors')
const koa2Common = require('koa2-common')
var router = require('koa-router')();

const indexRoute  = require('./router/router.js'); 
const SYSTEM  = require('./config.js'); 
indexRoute(router)
const app = new Koa()
const env = process.env.NODE_ENV || 'development'

// 打印日志
app.on('error', (err, ctx) => {
    console.log(err)
});
 
render(app, {
    root: path.join(__dirname, './view'),
    layout: 'template',
    viewExt: 'html',
    cache: true,
    debug: true
});
app
    .use(koa2Common())
    .use(cors({
        origin: SYSTEM.ORIGIN,
        headers: 'Origin, X-Requested-With, Content-Type, Accept',
        methods: ['GET', 'PUT', 'POST'],
        credentials: true,
    }))
    app.use(router.routes()).use(router.allowedMethods())
app.listen(3000, () => {
  console.log('成功启动 ，3000端口')
})


